package com.lyw;

import java.lang.reflect.Constructor;
import java.lang.reflect.Field;
import java.lang.reflect.Method;
import java.lang.reflect.Modifier;

public class Main {
    public static void main(String[] args) throws ClassNotFoundException, NoSuchMethodException, NoSuchFieldException {
        Class<Student> studentClass = Student.class;
        System.out.println(studentClass);
//        System.out.println("---------传统方式----------------");
//        //传统方式
//        Student student = new Student();
//        Class aClass = student.getClass();
//        System.out.println(aClass);
        System.out.println("=============forName==================");
        Class<?> aClass1 = Class.forName("com.lyw.Student");
        System.out.println(aClass1);
        System.out.println("============获取类名无包名=========");
        System.out.println(aClass1.getSimpleName());
        System.out.println("=========获取包名=============");
        System.out.println(aClass1.getPackageName());
        System.out.println("============获取父类名=========");
        Class<? super Student> superclass = studentClass.getSuperclass();
        System.out.println(superclass);
        System.out.println("=======获取数组========");
        Class<?>[] interfaces = studentClass.getInterfaces();
        for (Class aninterfaces :interfaces){
            System.out.println(aninterfaces.getName());
        }
        System.out.println("=====获取modified=========");
        int modifiers = studentClass.getModifiers();
        System.out.println(Modifier.toString(modifiers));
        System.out.println("=====获取无参constructor======");
        Constructor<Student> constructor = studentClass.getConstructor();
        System.out.println(constructor.getName());
        System.out.println("=====获取有参constructor======");
        Constructor<Student> constructor1 = studentClass.getDeclaredConstructor(String.class);
        Class [] parameterTypes = constructor1.getParameterTypes();
        for (Class parameterType:parameterTypes){
            System.out.println(parameterType);
        }
        System.out.println("======获取属性=======");
        Field field = studentClass.getDeclaredField("id");
        System.out.println(field.getType());
        Field [] declaredFiled=studentClass.getDeclaredFields();
        for (Field field1:declaredFiled){
            System.out.println(field1.toGenericString());
        }
        System.out.println("====获取方法======");
        Method[] method = studentClass.getDeclaredMethods();
        for (Method method1:method){
            System.out.println(method1.toGenericString());
            if ((Modifier.isPublic(method1.getModifiers()))){
                System.out.println("=====获取修饰符=======");
                System.out.println("public");
            }
        }
    }
}